package com.liangtian.mdm.system.user.util;

import com.liangtian.mdm.base.utils.CipherUtil;
import com.liangtian.mdm.base.utils.PlatForm;
import com.liangtian.mdm.base.utils.ReadSystemParam;
import com.liangtian.mdm.system.user.model.SysUser;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.servlet.http.HttpServletRequest;
import java.io.FileReader;
import java.util.List;

public class UserPassMd5 {
    //用户名密码加密
    public static List<SysUser> userPassEncrypt(HttpServletRequest request, List<SysUser> suLst, boolean sign) throws Exception {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("javascript");
        //String jsPath = request.getSession().getServletContext().getRealPath("/") +"js/md5/md5.js";
        //--edit by wangyk at 180628:上面的代码在weblogic下不好使，改成下面的代码
        FileReader reader = new FileReader(getMd5JsFilePath());   // 执行指定脚本
        engine.eval(reader);
        if (engine instanceof Invocable) {
            Invocable invoke = (Invocable) engine;    // 调用merge方法，并传入两个参数
            for (SysUser su : suLst) {
                String apiSysStr = ReadSystemParam.getValue("INIT_PASSWORD");
                String password = (String) invoke.invokeFunction("hex_md5", sign ? apiSysStr : su.getUserPassword().trim());
                String pwd = CipherUtil.createPwdEncrypt(su.getUserAccount().trim(), password, su.getUserSalt().trim());
                su.setUserPassword(pwd);
            }
        }
        return suLst;
    }

    public static String userPassEncryptToString(HttpServletRequest request, String userAccount, String userPass, String salt) {
        String retPass = "";
        try {
            ScriptEngineManager manager = new ScriptEngineManager();
            ScriptEngine engine = manager.getEngineByName("javascript");
            //--String jsPath = request.getSession().getServletContext().getRealPath("/") +"js/md5/md5.js";
            //--edit by wangyk at 180628:上面的代码在weblogic下不好使，改成下面的代码
            FileReader reader = new FileReader(getMd5JsFilePath());   // 执行指定脚本
            engine.eval(reader);
            if (engine instanceof Invocable) {
                Invocable invoke = (Invocable) engine;    // 调用merge方法，并传入两个参数
                String password = (String) invoke.invokeFunction("hex_md5", userPass);
                retPass = CipherUtil.createPwdEncrypt(userAccount, password, salt);
            }
        } catch (Exception e) {
            System.out.println("用户名为" + userAccount + "的用户获取加密后的密码失败---");
            e.printStackTrace();
        }
        return retPass;
    }

    /**
     * 方法等同于上面的方法，只是不再需要request参数
     *
     * @param userAccount
     * @param userPass
     * @param salt
     * @return
     */
    public static String userPassEncryptToStringNew(String userAccount, String userPass, String salt) {
        String retPass = "";
        try {
            ScriptEngineManager manager = new ScriptEngineManager();
            ScriptEngine engine = manager.getEngineByName("javascript");
            FileReader reader = new FileReader(getMd5JsFilePath());   // 执行指定脚本
            engine.eval(reader);
            if (engine instanceof Invocable) {
                Invocable invoke = (Invocable) engine;    // 调用merge方法，并传入两个参数
                String password = (String) invoke.invokeFunction("hex_md5", userPass);
                retPass = CipherUtil.createPwdEncrypt(userAccount, password, salt);
            }
        } catch (Exception e) {
            System.out.println("用户名为" + userAccount + "的用户获取加密后的密码失败---");
            e.printStackTrace();
        }
        return retPass;
    }

    private static String getMd5JsFilePath() {
        return PlatForm.getAbsolutePath() + "js/md5/md5.js";
    }
}
